package projectmanagement.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import projectmanagement.model.Person;
import projectmanagement.model.Project;
import projectmanagement.model.Task;
import projectmanagement.util.ApplicationSecurityManager;
import projectmanager.model.dao.PersonDao;
import projectmanager.model.dao.ProjectDao;
import projectmanager.model.dao.TaskDao;

public class EnterTaskController extends SimpleFormController{
	public static final String TASK_ID = "id";
	public static final String ACTION = "action";
	public static final String ACTION_NEW = "new";
	public static final String ACTION_EDIT = "edit";
	
	private TaskDao taskManager;
	private ProjectDao projectManager;
	private PersonDao personManager;
	
	private ApplicationSecurityManager applicationSecurityManager;

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		String id = request.getParameter(TASK_ID);
		String action = request.getParameter(ACTION);
		
		
		if (action != null && action.length() > 0){
			if (action.equals(ACTION_EDIT) && id != null && id.trim().length() > 0) {
				Task task = this.taskManager.getTaskById(Integer
						.parseInt(id));
				return task;
			}else if (action.equals(ACTION_NEW) && id == null){
				Task task = new Task();
				task.setCreationDate(new Date());
				return task;
			}
			else{
				throw new Exception();
			}
		}
		return null;
	}

	public void onBindAndValidate(HttpServletRequest request, Object command,
			BindException errors) throws Exception {
		if (errors.hasErrors())
			return;

		Task task = (Task) command;
		String action = request.getParameter(ACTION);

		if (action != null && action.length() > 0) {
			taskManager.save(task);
		} else {
			errors.reject("error.system");
		}
	}
	
	protected Map<String, Map<String, String>> referenceData(
			HttpServletRequest request) throws Exception {
		HashMap<String, String> projects = new HashMap<String, String>();
		HashMap<String, String> persons = new HashMap<String, String>();
		HashMap<String, String> statuses = new HashMap<String, String>();

		List<Project> projectList = projectManager.getProjectList();
		for (int i = 0; i < projectList.size(); i++) {
			projects.put(String.valueOf(projectList.get(i).getId()), projectList
					.get(i).getName());
		}
		
		List<Person> personList = personManager.getPersonList();
		for (int i = 0; i < personList.size(); i++) {
			persons.put(String.valueOf(personList.get(i).getId()), personList
					.get(i).getName()
					+ " " + personList.get(i).getSurname());
		}
		
		statuses.put("created", "Sukurta");
		statuses.put("started", "Pradeta");
		statuses.put("resolved", "Padaryta");
		statuses.put("closed", "Uzdaryta");

		HashMap<String, Map<String, String>> model = new HashMap<String, Map<String, String>>();
		model.put("projectList", projects);
		model.put("personList", persons);
		model.put("statusList", statuses);

		return model;
	}

	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		return new ModelAndView(getSuccessView());
	}

	public TaskDao getTaskManager() {
		return taskManager;
	}

	public void setTaskManager(TaskDao taskManager) {
		this.taskManager = taskManager;
	}

	public ProjectDao getProjectManager() {
		return projectManager;
	}

	public void setProjectManager(ProjectDao projectManager) {
		this.projectManager = projectManager;
	}

	public PersonDao getPersonManager() {
		return personManager;
	}

	public void setPersonManager(PersonDao personManager) {
		this.personManager = personManager;
	}

	public ApplicationSecurityManager getApplicationSecurityManager() {
		return applicationSecurityManager;
	}

	public void setApplicationSecurityManager(
			ApplicationSecurityManager applicationSecurityManager) {
		this.applicationSecurityManager = applicationSecurityManager;
	}
}
